#include "pch.h"
#include "DDS_Message.h"
#include "Message.h"
#include "common.h"
#include "lib/thirdparty/extcode.h"
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fstream>
void logFile(const std::string &file_name, const std::string &msg) {
    /*
    std::ofstream ofs;
    ofs.open(file_name, std::ios::app | std::ios::binary);
    if (ofs.is_open()) {
        ofs << msg << std::endl;
        ofs.close();
    }*/
    FILE* fp = nullptr;
    if(0 != fopen_s(&fp, file_name.c_str(), "wb+")){
        return;
    }
    fwrite(msg.c_str(), 1, msg.size(), fp);
    fclose(fp);
}
void logFile(const std::string& file_name, DDS_PARAMETER::LV_Message* lv_msg) {

    std::ofstream ofs;
    ofs.open(file_name, std::ios::app | std::ios::binary);
    if (ofs.is_open()) {
        ofs << lv_msg->send_timestamp << " "<< lv_msg->recv_timestamp<<" "<< lv_msg->index<<" "<< lv_msg->msg_len << " " << std::endl;
        ofs.close();
    }
}
using namespace eprosima::fastdds::dds;
DDS_Message::SubscriberListener::SubscriberListener(): matched_(0), call_back_ref_(0)
{
}

DDS_Message::SubscriberListener::~SubscriberListener()
{
}

void DDS_Message::SubscriberListener::on_data_available(eprosima::fastdds::dds::DataReader* reader)
{
    SampleInfo info;
    Message message;
    while (reader->take_next_sample(&message, &info) == ReturnCode_t::RETCODE_OK)
    {
        //std::this_thread::sleep_for(std::chrono::microseconds(100));
        if (info.instance_state == ALIVE_INSTANCE_STATE)
        {
            last_msg_ = message.message();
            timestamp_ = message.timestamp();
            char *msg = const_cast<char*>(message.message().data());
            
            DDS_PARAMETER::LV_Message *lv_msg = (DDS_PARAMETER::LV_Message*)malloc(sizeof(DDS_PARAMETER::LV_Message) + message.message().size());
            lv_msg->index = message.index();
            lv_msg->msg_len = message.message().size();
            lv_msg->send_timestamp = message.timestamp();
            lv_msg->recv_timestamp = get_timestamp();
            
            memcpy(lv_msg->data, message.message().data(), message.message().size());
            //lv_msg.context = const_cast<char*>(last_msg_.data());
            //char* res = (char*)malloc(sizeof(DDS_PARAMETER::LV_Message));
            if (call_back_ref_)PostLVUserEvent(call_back_ref_, &lv_msg);
            //logFile("C:/test_log/recv_msg.log", lv_msg);
            std::cout << message.message() << std::endl;

        }
    }
}

void DDS_Message::SubscriberListener::on_subscription_matched(eprosima::fastdds::dds::DataReader* reader, const eprosima::fastdds::dds::SubscriptionMatchedStatus& info)
{
    if (info.current_count_change == 1)
    {
        matched_ = info.total_count;
        std::cout << "Subscriber matched." << std::endl;
    }
    else if (info.current_count_change == -1)
    {
        matched_ = info.total_count;
        std::cout << "Subscriber unmatched." << std::endl;
    }
    else
    {
        std::cout << info.current_count_change
            << " is not a valid value for SubscriptionMatchedStatus current count change" << std::endl;
    }
}

void DDS_Message::SubscriberListener::set_callback(uint32_t ref)
{
    call_back_ref_ = ref;
}

bool DDS_Message::SubscriberListener::is_matched()
{
    return matched_ > 0;
}

std::string DDS_Message::SubscriberListener::get_last_msg()
{
    return last_msg_;
}

uint64_t DDS_Message::SubscriberListener::get_timestamp()
{
    return timestamp_;
}
